﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace jQuery.Validation.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class NumberAttribute : ValidationAttribute
    {
        public const string RegExString = @"^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$";

        public NumberAttribute()
        {
        }

        public override bool IsValid(object value)
        {
            if (value == null)
                return true;

            if (value is double || value is float || value is decimal || value is int || value is Int32 
                || value is Int16 || value is Int64 || value is Single)
            {
                return true;
            }

            if (value is string)
            {
                Regex regEx = new Regex(RegExString);
                return regEx.IsMatch(value.ToString());
            }

            return false;
            
        }
    }
}
